package com.yjt.fresh;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.tencent.bugly.beta.Beta;
import com.umeng.socialize.UMShareAPI;
import com.yjt.fresh.adpater.AdpTabLayoutViewPager;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.fragment.FrgInformation;
import com.yjt.fresh.fragment.FrgPersonal;
import com.yjt.fresh.fragment.Frg_Home;
import com.yjt.fresh.fragment.FrgCompany;
import com.yjt.fresh.fragment.FrgHistory;
import com.yjt.fresh.utils.ActivityStackControlUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.jzvd.JZVideoPlayer;

public class MainActivity extends BaseSimpleActivity {
    private String[] mTabTitles = {"首页", "资讯", "溯源","企业", "我的"};
    private int[] mTabIcons = new int[]{R.drawable.selector_tab_home, R.drawable.selector_tab_information
            , R.drawable.selector_tab_history, R.drawable.selector_tab_company,R.drawable.selector_tab_personal};

    private Frg_Home mFrgHome;
    private FrgInformation mFrgInformation;
    private FrgHistory mFrgHistory;
    private FrgCompany mFrgCompany;
    private List<Fragment> mFrgList;

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private AdpTabLayoutViewPager mAdpTabLayout;
    private FrgPersonal mFrgPersonal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void initViews() {
        mFrgList = new ArrayList<>();
        mFrgHome = new Frg_Home();
        mFrgList.add(mFrgHome);

        mFrgInformation = new FrgInformation();
        mFrgList.add(mFrgInformation);

        mFrgHistory = new FrgHistory();
        mFrgList.add(mFrgHistory);

        mFrgCompany = new FrgCompany();
        mFrgList.add(mFrgCompany);

        mFrgPersonal = new FrgPersonal();
        mFrgList.add(mFrgPersonal);

        mViewPager = getViewById(R.id.main_viewpager);
        mTabLayout = getViewById(R.id.main_tablayout);

        mAdpTabLayout = new AdpTabLayoutViewPager(getSupportFragmentManager(), Arrays.asList(mTabTitles), mFrgList);
        mViewPager.setAdapter(mAdpTabLayout);
        mViewPager.setHorizontalFadingEdgeEnabled(false);
        mViewPager.setHorizontalScrollBarEnabled(false);
        mViewPager.setOffscreenPageLimit(5);
        mTabLayout.setupWithViewPager(mViewPager);
        setupTabIcons();
        mViewPager.setCurrentItem(0, false);
        mViewPager.addOnPageChangeListener(mOnPageChangeListener);
    }

    ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position == 0 || position == 1 || position == 3 || position == 4){
                GSYVideoManager.onPause();
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };
    @Override
    protected void initEvent() {
        ArrayList arrayList = new ArrayList();
    }

    @Override
    protected void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    private void setupTabIcons() {
        for (int i = 0; i < mTabTitles.length; i++) {
            mTabLayout.getTabAt(i).setCustomView(getTabView(i));
        }
    }

    public View getTabView(int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.uc_tab_home, null);
        TextView tabTitleTv = (TextView) view.findViewById(R.id.home_tab_title);
        tabTitleTv.setText(mTabTitles[position]);
        ImageView img_title = (ImageView) view.findViewById(R.id.home_tab_icon);
        img_title.setBackgroundResource(mTabIcons[position]);
        return view;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            if ((System.currentTimeMillis() - exitTime) > 2000)  //System.currentTimeMillis()无论何时调用，肯定大于2000
            {
                Toast.makeText(mContext, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_HOME) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
